﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;

namespace EIA.SLClient.PlatManage.PMMgr
{
    public partial class SR_StaffNamesView : UserControl
    {
        public SR_StaffNamesView()
        {
            // 为初始化变量所必需
            InitializeComponent();
            if (Utility.DesignModeUtils.IsInDesignMode)
                txtNames.Text = "[Display Staff's Names]";
        }

        #region Staffs (DependencyProperty)

        /// <summary>
        /// 该依赖属性的描述
        /// </summary>
        public IEnumerable<EIA.Basic.Model.TStaff> Staffs
        {
            get { return (IEnumerable<EIA.Basic.Model.TStaff>)GetValue(StaffsProperty); }
            set { SetValue(StaffsProperty, value); }
        }
        public static readonly DependencyProperty StaffsProperty =
            DependencyProperty.Register("Staffs", typeof(IEnumerable<EIA.Basic.Model.TStaff>), typeof(SR_StaffNamesView),
            new PropertyMetadata(null, new PropertyChangedCallback(OnStaffsChanged)));

        private static void OnStaffsChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
        {
            // 获取自身的引用
            SR_StaffNamesView source = (SR_StaffNamesView)sender;

            // 处理逻辑
            IEnumerable<EIA.Basic.Model.TStaff> newValue = (IEnumerable<EIA.Basic.Model.TStaff>)args.NewValue;
            source.Display();
        }

        #endregion

        #region DisplayNumber (DependencyProperty)

        /// <summary>
        /// 该依赖属性的描述
        /// </summary>
        public int DisplayNumber
        {
            get { return (int)GetValue(DisplayNumberProperty); }
            set { SetValue(DisplayNumberProperty, value); }
        }
        public static readonly DependencyProperty DisplayNumberProperty =
            DependencyProperty.Register("DisplayNumber", typeof(int), typeof(SR_StaffNamesView),
            new PropertyMetadata(3, new PropertyChangedCallback(OnDisplayNumberChanged)));

        private static void OnDisplayNumberChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
        {
            // 获取自身的引用
            SR_StaffNamesView source = (SR_StaffNamesView)sender;

            // 处理逻辑
            int newValue = (int)args.NewValue;
        }

        #endregion

        private void Display()
        {
            List<string> names = new List<string>();
            if (Staffs != null)
            {
                int count = 0;
                foreach (var s in Staffs)
                {
                    if (count >= DisplayNumber) break;
                    names.Add(s.StfName);
                    count++;
                }
            }
            txtNames.Text = string.Join(", ", names);
            if (names.Count > DisplayNumber) txtNames.Text += "...";
        }
    }
}